/*
 * @Author: ChunLai
 * @Date: 2022-08-26 15:34:35
 * @LastEditTime: 2025-05-21 11:07:53
 * @Description: 全局公共方法
 * @FilePath: \02.bldinsure\src\methods\globals.js
 */
import store from "@/store/index";

export const userAge = (date, addVal = 0) => {
  if (!date) {
    return 0;
  }
  let birthdays = new Date(date.replace(/-/g, "/"));
  // 生日加一天数据配置
  try {
    const dis_state = store?.state?.dictionary || {};
    if (dis_state.birthAddValue != null) {
      addVal = dis_state.birthAddValue;
    }
  } catch (e) {
    console.error("Error accessing store.state.dictionary:", e);
  }
  // 生日加一天
  if (addVal !== 0) {
    birthdays = addDays(birthdays, addVal);
  }
  // 计算年龄
  const age = calculateAge(birthdays);
  return age;
};
// 辅助函数：日期加减天数
const addDays = (date, days) => {
  const result = new Date(date.getTime());
  result.setDate(result.getDate() + days);
  return result;
};
// 辅助函数：计算年龄
const calculateAge = (birthDate) => {
  const today = new Date();
  let age = today.getFullYear() - birthDate.getFullYear();
  const monthDiff = today.getMonth() - birthDate.getMonth();
  const dayDiff = today.getDate() - birthDate.getDate();
  if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
    age -= 1;
  }
  return age;
};

export const removeToViewEle = (eleId) => {
  document.getElementById(eleId).scrollIntoView({
    behavior: "smooth",
    block: "center",
  });
};

export const toViewEleClass = (className) => {
  let target = document.querySelector(className);
  if (target) {
    target.scrollIntoView({
      behavior: "smooth",
      block: "center",
    });
  }
};

export const isMinorBaby = (item) => {
  const pageState = JSON.parse(sessionStorage.bld_insure).page,
    ageDemo = pageState.minorAge;
  let res = false;
  const age = userAge(item.birthDate);
  if (age < ageDemo) {
    res = true;
  }
  return res;
};
// this.$GlobalFuns.isMinorBaby

// import {userAge} from "@/methods/globals.js";
